package server;

import java.io.IOException;
import java.io.InputStream;

/**
 *
 * 把请求信息封装为Request对象(根据InputStream输入流封装）
 * @program: minicat
 * @description
 * @author: luffy
 * @create: 2020-08-02 21:16
 */
public class Request {
    /**
     * 请求方式，比如：get、post
     */
    private String method;

    private String url;

    /**
     * 属性从输入流中解析出来
     */
    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * 构造器，输入流传入
     * @param inputStream
     */
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        //从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();

        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);

        //获取第一行
        String firstLintStr = inputStr.split("\\n")[0];
        String[] strings = firstLintStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];

    }
}
